package com.locationbasedagent.server;

import java.util.List;

import com.locationbasedagent.annotation.ServiceMethod;
import com.locationbasedagent.shared.TaskChange;


public class LocationBasedAgentService {
	private DataStore db = new DataStore();

	@ServiceMethod
	public LocationBasedAgentUser createLocationBasedAgentUser(String userID, String firstname, String lastname, 
			String phonenumber, boolean isAgent, String hometown) {
		return db.addUser(userID, firstname, lastname, phonenumber, isAgent, hometown);
	}

	@ServiceMethod
	public LocationBasedAgentUser readLocationBasedAgentUser(Long id) {
		// TODO Auto-generated method stub
		return null;
	}

	@ServiceMethod
	public LocationBasedAgentUser updateLocationBasedAgentUser(
			LocationBasedAgentUser locationbasedagentuser) {
		return db.updateUserInfo(locationbasedagentuser);
	}

	@ServiceMethod
	public void deleteLocationBasedAgentUser(
			LocationBasedAgentUser locationbasedagentuser) {
		// TODO Auto-generated method stub

	}

	@ServiceMethod
	public List<LocationBasedAgentUser> queryLocationBasedAgentUsersByID(String userID) {
		return db.findUserByID(userID);
	}

	@ServiceMethod
	public HouseListing createHouseListing(String name, String address, String city, String zip,
			String agentID, String agentNumber, String latitude, String longitude, String price, String photo1, String photo2, 
			String photo3, String photo4, String photo5, String video) {
		List<LocationBasedAgentUser> listOfUsers = db.findUsers();
		
		DataStore.sendC2DMUpdate(TaskChange.ADDED + TaskChange.SEPARATOR + "A new " + name + " has just been listed!", listOfUsers);
		return db.addHouseListing(name, address, city, zip, agentID, agentNumber, latitude, longitude, price, 
				photo1, photo2, photo3, photo4, photo5, video);
	}

	@ServiceMethod
	public HouseListing readHouseListing(Long id) {
		// TODO Auto-generated method stub
		return null;
	}

	@ServiceMethod
	public HouseListing updateHouseListing(Long id, String name, String address, String city, String zip,
			String agentID, String agentNumber, String latitude, String longitude, String price, String photo1, String photo2, 
			String photo3, String photo4, String photo5, String video) {
		List<HouseListing> listOfHouses = db.findHouseByID(id.toString());
		
		HouseListing houselisting = listOfHouses.get(0);
		
		return db.updateHouseListing(houselisting, name, address, city, zip,
			agentID, agentNumber, latitude, longitude, price, photo1, photo2, 
			photo3, photo4, photo5, video);
	}

	@ServiceMethod
	public void deleteHouseListing(Long id) {
		db.deleteHouseListing(id);

	}

	
	 @ServiceMethod
	public List<HouseListing> queryHouseListings(String operation, String id, String name, String address, String city, String zip,
			String agentID, String agentNumber, String latitude, String longitude, String price, String photo1, String photo2, 
			String photo3, String photo4, String photo5, String video) {
		if(operation.equalsIgnoreCase("UPDATE")) {
			List<HouseListing> list = db.findHouseByID(id);
			HouseListing houseListing = list.get(0);
			
			 db.updateHouseListing(houseListing, name, address, city, zip,
						agentID, agentNumber, latitude, longitude, price, photo1, photo2, 
						photo3, photo4, photo5, video);
			 return db.findAllWhere("", "", "", "");
			 	 
		}
		if(operation.equalsIgnoreCase("POPULATE")) {
			db.populateTheDataStore();
		}
		
		return db.findAllWhere(address, city, zip, agentID);
	}
	
		
		
	 
	
	/*
	@ServiceMethod
	public List<HouseListing> queryHouseListings(String address, String city, String zip, String sellingID) {
		return db.findAllWhere(address, city, zip, sellingID);
	}
	*/


}
